package com.boss.salary.controller;

import com.alibaba.fastjson.JSONObject;
import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.User;
import com.boss.common.entity.WfActionDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.PersonInfo;
import com.boss.salary.entity.SalaryBaseRelevanceDTO;
import com.boss.salary.service.IRedisService;
import com.boss.salary.service.ISalaryBaseRelevanceService;
import com.boss.salary.workflow.service.ISalaryWorkFlowService;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <br>Title:
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@RestController
public class Login  extends BasicinfoBaseController {

    private final static Logger logger = LoggerFactory.getLogger(Login.class);

    @Autowired
    private ICommonService commonService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private ISalaryWorkFlowService iSalaryWorkFlowService;

    @Autowired
    private ISalaryBaseRelevanceService salaryBaseRelevanceService;

    @GetMapping("/boss_Salary_vue/getWorkStatus")
    public ApiResultDTO getWorkStatus(@RequestHeader("X-Token") String tokenid,String menuid,String bizkey,String status) {
        try {
            UserDTO userDTO=getUser(tokenid);
            WfActionDTO wfActionDTO = new WfActionDTO();
            List<String> statusList=new ArrayList<>();
            statusList.add(status);
            // 菜单id
            wfActionDTO.setMenuguid(menuid);
            List<String> bizkeyList=new ArrayList<>();
            bizkeyList.add(bizkey);
            wfActionDTO.setProvince(userDTO.getProvince());
            List<Map> requesMap=iSalaryWorkFlowService.queryWorkflowInfoStatus(userDTO,wfActionDTO,statusList,bizkeyList);
            return ApiResultDTO.success(requesMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResultDTO.error("");
    }
    @GetMapping("/boss_Salary_vue/jd")
    public void jd(@RequestHeader("X-Token") String tokenid,String sum_id,String amount) {
        try {
            List<Map<String,Object>> mapList =new ArrayList<>();
            Map<String,Object> map=new HashMap<>();
            //sum_id amount

            UserDTO userDTO=getUser(tokenid);
            map.put("bgt_id",18563);
            map.put("amt",100);
            map.put("agency_id",111111);
            mapList.add(map);
            Map<String,Object> map1=new HashMap<>();
            map1.put("bgt_id",2917);
            map1.put("amt",200);
            map1.put("agency_id",111111);
            mapList.add(map1);
            Map<String,Object> map2=new HashMap<>();
            map2.put("bgt_id",105834);
            map2.put("amt",300);
            map2.put("agency_id",111111);
            mapList.add(map2);
            commonService.batchGocpurbudgetThaw(mapList,userDTO.getProvince(),userDTO.getYear()+"",null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/boss_Salary_vue/user/login")
    public ApiResultDTO login(@RequestBody User user) {
        //String tokenid="fa623f0bee6bd8abec5e0798fb157a1e";
        //Map<String, String> map = new HashMap<String, String>();
        //map= commonService.getUserByToken(tokenid);
        String username=user.getUsername();
        String password=user.getPassword();
        PersonInfo personInfo=new PersonInfo();
        if("admin".equals(username)){
            user.setName("刘勇");
            user.setAgencyId("0067CA44032B4EF1B4FBC12A4BEC3E68");
            user.setAgencyCode("101002");
            user.setAgencyname("中共吉林省委机要局");
            user.setToken("97ddf29aa1d9942b4e05e01a24deebea");

        }else if("editor".equals(username)){
            user.setName("张韩山");
            user.setAgencyId("BF9E94A4D4CF4DF19958025DB5923D1D");
            user.setAgencyCode("101002002");
            user.setAgencyname("吉林省密钥管理中心");
            user.setToken("97ddf29aa1d9942b4e05e01a24deebea");
        }
        return ApiResultDTO.success(user);
    }

    /**
     * 根据token获取用户信息
     * @param tokenid
     * @return
     */
    @PostMapping("/boss_Salary_vue/user/tokenLogin")
    public ApiResultDTO loginByToken(@RequestHeader("X-Token") String tokenid) {
        User user = new User();
        user.setToken(tokenid);
        try {
            UserDTO userDTO=getUser(tokenid);
            user= commonService.getUserByToken(user);
        } catch (Exception e) {
            return ApiResultDTO.error(ERROR_RSCODE,"登录失败！");
        }
        return ApiResultDTO.success(user);
    }

    /**
     * 根据token获取菜单树
     * @param tokenid
     * @return
     */
    @GetMapping("/getMenuTree")
    public ApiResultDTO getMenuTree(@RequestHeader("X-Token") String tokenid) {
        User user = new User();
        user.setToken(tokenid);
        //通过tokenid获取菜单
        Map<String, String> map =commonService.getMenuTree(user);
        return ApiResultDTO.success(map);
    }

    @GetMapping("/boss_Salary_vue/user/info")
    public ApiResultDTO userInfo(String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("roles", "admin");
        map.put("name", "超级管理员");
        map.put("avatar", "admin");
        map.put("introduction", "admin");
        return ApiResultDTO.success(map);
    }
    /**
     * 查看支付生成进度
     * @param uuid
     * @return
     */
    @GetMapping("/deletePaymentPercent")
    public ApiResultDTO  getPaymentPercent(@RequestParam(value = "uuid") String uuid){

            redisService.del(uuid);

        return ApiResultDTO.success(1);
    }

    @GetMapping("/deleteRedis")
    public ApiResultDTO  deletePaymentPercent(@RequestHeader("X-Token") String tokenid,@RequestParam(value = "uuid") String uuid){
        try {
            UserDTO userDTO=getUser(tokenid);
            StringBuffer redisKey=new StringBuffer();
            if("generateSalary".equals(uuid)){
                redisKey.append(userDTO.getProvince()+"generateSalary") ;//清除生成工资数据锁
            }else if("lunarKnot".equals(uuid)){
                redisKey.append(userDTO.getProvince()+"lunarKnot") ;//清除月结锁batchLock
            }else if("batchLock".equals(uuid)){
                redisKey.append(userDTO.getProvince()+"batchLock") ;//清除批量审核锁
            }else if("submitLock".equals(uuid)){
                redisKey.append(userDTO.getProvince()+"submitLock") ;//清除批量上报锁
            }
            Object oj=redisService.get(redisKey.toString());
            if(SysUtils.isNullOrEmpty(oj)){
                return ApiResultDTO.error(ERROR_RSCODE,"没有需要清除的执行锁");
            }else{
                redisService.del(redisKey.toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        return ApiResultDTO.success(1);
    }
    /**
     * 退出
     * @param tokenid
     * @return
     */
    @PostMapping("/boss_Salary_vue/user/logout")
    public ApiResultDTO logout(@RequestHeader("X-Token") String tokenid) {
        logger.info(" ==== logout ====", tokenid);
        Map<String, Object> map =commonService.logout(tokenid);
        if ("100000".equals(map.get("rscode"))) {
            return ApiResultDTO.error("113999", JSONObject.toJSON(map.get("data")).toString());
        }
        return ApiResultDTO.success(map);
    }

//    @PostMapping("/getPayMent")
//    public ApiResultDTO getPayMent(@RequestHeader("X-token") String tokenid,
//                                   @RequestBody List<String> bgtids) {
//        String rest="";
//        try {
//            UserDTO  userDTO = getUser(tokenid);
//            //Map<String,BigDecimal> bgtcanuseMapList = commonService.getPayMent(bgtids,userDTO);
//            List<Map<String, Object>> payRequestList=new ArrayList<>();
//             rest=generateSalaryDataService.checkPayment(bgtids,userDTO);
//            List<Map<String, Object>> bgtcanuseMapList=generateSalaryDataService.checkIsEnough(bgtids,userDTO,payRequestList,);
//            logger.info("返回信息"+rest);
//            logger.info("返回传入信息："+payRequestList.toString());
//            return ApiResultDTO.success(bgtcanuseMapList);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return ApiResultDTO.error("999",rest);
//    }

    @PostMapping("/sentPayMent")
    public ApiResultDTO sentPayMent(@RequestHeader("X-token") String tokenid,
                            @RequestBody List<Map<String, Object>> paramMap ) {
        Map<String, String> map =commonService.sentPayMent(paramMap, tokenid);
        return ApiResultDTO.success(map);
    }

    @PostMapping("/sentBigPayMent")
    public ApiResultDTO sentBigPayMent(@RequestHeader("X-token") String tokenid,
                                    @RequestBody List<Map<String, Object>> paramMap ) {
        Map<String, String> map =commonService.sentPaymentAsync(paramMap, tokenid);
        return ApiResultDTO.success(map);
    }

    @PostMapping("/nullifyPayMent")
    public ApiResultDTO nullifyPayMent(@RequestHeader("X-token") String tokenid,
                                    @RequestBody List<String> paramMap ) {
        Map<String, String> map =commonService.nullifyPayMent(paramMap, tokenid);
        return ApiResultDTO.success(map);
    }
    @GetMapping("/salaryBaseRelevance")
    public ApiResultDTO salaryBaseRelevance(@RequestHeader("X-token") String tokenid,
                                            @RequestParam(value = "page") String page,
                                            @RequestParam(value = "limit") String limit) {
        try {
            UserDTO userDTO = getUser(tokenid);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            return ApiResultDTO.success(salaryBaseRelevanceService.selectSalaryBaseRelevance(paginationDTO,userDTO.getProvince()));
        }catch (Exception e){
            return ApiResultDTO.error(ERROR_RSCODE,e.getMessage());
        }
    }

    @PostMapping("/insertSalaryBaseRelevance")
    public ApiResultDTO insertSalaryBaseRelevance(@RequestHeader("X-token") String tokenid,
                                                  @RequestBody List<SalaryBaseRelevanceDTO> srList) {
        try {
            UserDTO userDTO = getUser(tokenid);
            salaryBaseRelevanceService.batchReplaceSalaryBaseRelevance(srList,userDTO);
            return ApiResultDTO.success();
        }catch (Exception e){
            return ApiResultDTO.error(ERROR_RSCODE,e.getMessage());
        }
    }
}
